package main

import (
	"MaterialIdentification/svc"
	"github.com/gin-gonic/gin"
	"io"
	"log/slog"
	"net/http"
	"os"
)

type Iddto struct {
	Id int
}

func Download(context *gin.Context) {
	//i := Iddto{}
	//context.BindJSON(&i)
	//fmt.Println(i.Id)

	value := context.Query("id")

	l := Logs{}
	err2 := svc.Db.Find(&l, value).Error
	if err2 != nil {
		slog.Error("数据库异常", "err", err2)
		context.String(http.StatusInternalServerError, "系统异常")
		return
	}

	imagePath := "./pictrues/" + l.Originpicture

	file, err := os.Open(imagePath)
	if err != nil {
		context.String(http.StatusInternalServerError, "读文件异常: %s", err)
		return
	}
	defer file.Close()

	context.Header("Content-Type", "image/jpeg")

	_, err = io.Copy(context.Writer, file)
	if err != nil {
		context.String(http.StatusInternalServerError, "发送文件异常: %s", err)
		return
	}
}
